package com.begye.smartloan.apptest.tool

import org.json.JSONArray
import org.json.JSONObject

object SysConfigTool {

    fun updateSysConfig(jsonObject: JSONObject) {
        SpTool.save(SysConfigTool::class.java.simpleName, jsonObject.toString())
    }

    fun filterYingYongTongZhi(): String {
        return try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val notice = jsonObject1.optString("platformNotice", "")
            notice
        } catch (e: Exception) {
            ""
        }
    }

    fun filterYingHuanKuanImportant(): String {
        return try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val notice = jsonObject1.optString("repayImportant", "")
            notice
        } catch (e: Exception) {
            ""
        }
    }

    fun filterJuJueDays(): String {
        return try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val days = jsonObject1.optString("rejectPeriod", "30")
            days
        } catch (e: Exception) {
            "30"
        }
    }

    fun filterMaxJinE(): String {
        return try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val jinE = jsonObject1.optString("maxJinE", "200000")
            jinE
        } catch (e: Exception) {
            "200000"
        }
    }

    fun filterStepJinE(): String {
        return try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val jinE = jsonObject1.optString("stepJinE", "1000")
            jinE
        } catch (e: Exception) {
            "1000"
        }
    }

    fun filterQiDongYeQuanXiang(): List<String> {
        return try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val jsonObject2 = jsonObject1.optJSONObject("permissions") ?: JSONObject()
            val array1 = jsonObject2.optJSONArray("permission") ?: JSONArray()

            val list = mutableListOf<String>()
            for (index in 0 until array1.length()) {
                val item = array1.optString(index)
                if (item.isNotBlank()) {
                    list.add(item)
                }
            }
            list
        } catch (e: Exception) {
            emptyList<String>()
        }
    }

    fun filterShouYeQuanXiang(): List<String> {
        return try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val jsonObject2 = jsonObject1.optJSONObject("permissions") ?: JSONObject()
            val array1 = jsonObject2.optJSONArray("permission") ?: JSONArray()
            val array2 = jsonObject2.optJSONArray("home") ?: JSONArray()

            val list = mutableListOf<String>()
            for (index in 0 until array1.length()) {
                val item = array1.optString(index)
                if (item.isNotBlank()) {
                    if (!list.contains(item)) {
                        list.add(item)
                    }
                }
            }

            for (index in 0 until array2.length()) {
                val item = array2.optString(index)
                if (item.isNotBlank()) {
                    if (!list.contains(item)) {
                        list.add(item)
                    }
                }
            }
            list
        } catch (e: Exception) {
            emptyList<String>()
        }
    }

    fun filterBangKaChannel(query: String): JSONObject? {
        return try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val array = jsonObject1.optJSONArray("walletChannels") ?: JSONArray()
            for (index in 0 until array.length()) {
                val item = array.optJSONObject(index)
                if (item != null) {
                    val value = item.optString("value", "")
                    if (value == query) {
                        return JSONObject(item.toString())
                    }
                }
            }
            null
        } catch (e: Exception) {
            null
        }
    }

    fun filterBangKaChannels(): JSONArray {
        return try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val array = jsonObject1.optJSONArray("walletChannels") ?: JSONArray()
            for (index in 0 until array.length()) {
                val item = array.optJSONObject(index)
                if (item != null) {
                    val desc = item.optString("desc", "")
//                    val value = item.optString("value", "")
//                    val logo = item.optString("logo", "")
                    item.put("title", desc)
                }
            }
            array
        } catch (e: Exception) {
            JSONArray()
        }
    }

    fun filterExtraBangKaStep(): List<String> {
        return emptyList()
//        return try {
//            val list = mutableListOf<String>()
//            val text = SpTool.get(SysConfigTool::class.java.simpleName)
//            val jsonObject = JSONObject(text)
//            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
//            val extraSteps = jsonObject1.optJSONArray("extraSteps") ?: JSONArray()
//            for (index in 0 until extraSteps.length()) {
//                val item = extraSteps.optString(index) ?: ""
//                if (item.isNotBlank()) {
//                    list.add(item)
//                }
//            }
//            return list
//
//        } catch (e: Exception) {
//            emptyList()
//        }
    }

    fun filterTestAccount(mobile: String): JSONObject? {
        return try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val accList = jsonObject1.optJSONArray("accList") ?: JSONArray()
            for (index in 0 until accList.length()) {
                val item = accList.optJSONObject(index) ?: JSONObject()
                val accM = item.optString("accM", "")
                val accTk = item.optString("accTk", "")
                val accUid = item.optString("accUid", "")
                if (mobile == accM) {
                    return JSONObject().apply {
                        put("token", accTk)
                        put("mobile", accM)
                        put("userId", accUid)
                        put("isAdSource", "NO")
                    }
                }
            }
            return null

        } catch (e: Exception) {
            null
        }
    }

    fun filterContactUs(): List<JSONObject> {
        val list = mutableListOf<JSONObject>()
        try {
            val text = SpTool.get(SysConfigTool::class.java.simpleName)
            val jsonObject = JSONObject(text)

            val jsonObject1 = jsonObject.optJSONObject("customize") ?: JSONObject()
            val mobile = jsonObject1.optString("mobile")
            val email = jsonObject1.optString("email")

            val mobileList = mobile.split(",")
            for (ele in mobileList) {
                if (ele.isNotBlank()) {
                    list.add(JSONObject().apply {
                        put("value", ele)
                        put("type", 1)
                    })
                }
            }

            val emailList = email.split(",")
            for (ele in emailList) {
                if (ele.isNotBlank()) {
                    list.add(JSONObject().apply {
                        put("value", ele)
                        put("type", 0)
                    })
                }
            }

        } catch (e: Exception) {
            //
        }

        return list
    }

}